import { Controller } from "../../mvc/core/Controller";
import { DataEvent } from "../events/DataEvent";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { WindowEvent } from "../../component/window/WindowEvent";
import { GameProxy } from "../mvc/GameProxy";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SDayRechargeInfo } from "../../../Message/Public/SDayRechargeInfo";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { DateUtil } from "../../common/tools/DateUtil";
import { ClockManager } from "../manager/ClockManager";
import { NewYearCountGiftBagIcon } from "../view/newYearCountGiftBag/NewYearCountGiftBagIcon";
import { IView } from "../../mvc/interfaces/IView";
import { NewYearCountGiftBagModule } from "../../../modules/NewYearCountGiftBagModule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
type int = number;
//class NewYearCountGiftBagController
    
    export  class NewYearCountGiftBagController extends Controller
    {
       

        /* internal  */onModuleShowHandler(arg1: DataEvent): void
        {
            if (view.isHide) 
            {
                view.show();
                Dispatcher.dispatchEvent(new DataEvent(EventName.NewYearItemRendererUpdate));
            }
            else 
            {
                view.hide();
            }
            return;
        }

        /* internal  */closeModule(arg1: WindowEvent): void
        {
            cache.newYearCount.hasFind = false;
            return;
        }

        /* internal  */onHasFindItemHandler(arg1: DataEvent): void
        {
            view.update(arg1.data);
            return;
        }

        /* internal  */onGetDayRechargeGiftBagHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.roleProxy.getDayRechargeGiftBag(loc1);
            return;
        }

        /* internal  */onDayRechargeInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SDayRechargeInfo;
            var loc2=cache.active.getActiveByCode(EActiveType._EActiveTypeDayRechargeGiftBag);
            var loc3;
            if ((loc3 = cache.active.isActiveInEffect(EActiveType._EActiveTypeDayRechargeGiftBag)) && loc2) 
            {
                cache.newYearCount.hasFind = false;
                cache.newYearCount.dayRecharge = loc1.dayRecharge;
                cache.newYearCount.hadGetCodes = loc1.hadGetCodes;
                cache.newYearCount.day = DateUtil.getDayDis(loc2.startDt, ClockManager.instance.nowDate) + 1;
                cache.newYearCount.surplusDay = DateUtil.getDayDis(ClockManager.instance.nowDate, loc2.endDt) + 1;
                if (this.btn.isHide) 
                {
                    this.btn.show();
                }
                if (_view && !view.isHide) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.NewYearItemRendererUpdate));
                }
                ClockManager.instance.addDateCall(loc2.endDt, this.removeActive);
            }
            return;
        }

        /* internal  */removeActive(): void
        {
            this.btn.hide();
            if (_view && !view.isHide) 
            {
                view.hide();
            }
            return;
        }

        /* internal  */get btn(): NewYearCountGiftBagIcon
        {
            if (!this._btn) 
            {
                this._btn = new NewYearCountGiftBagIcon();
            }
            return this._btn;
        }

        protected /* override */ initView(): IView
        {
            if (!this._module) 
            {
                this._module = new NewYearCountGiftBagModule();
                this._module.addEventListener(WindowEvent.CLOSE, this.closeModule);
            }
            return this._module;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.NewYearCountGiftBagModuleShow, this.onModuleShowHandler);
            Dispatcher.addEventListener(EventName.NewYearGetDayRechargeGiftBag, this.onGetDayRechargeGiftBagHandler);
            Dispatcher.addEventListener(EventName.NewYearCountGiftBagHasFindItem, this.onHasFindItemHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicDayRechargeInfo, this.onDayRechargeInfoHandler);
            return;
        }

        private /* var */_btn: NewYearCountGiftBagIcon;

        private /* var */_module: IView;
    }
